axios.defaults.headers.common["SAT"] = "1234567";
axios.defaults.headers.common["TOKEN"] = localStorage.getItem("token");

function Admin() {}
Admin.prototype = {
	constuctor: Admin,
	//添加头像
	heade(file, callback) {
		let qaq;
		document.querySelector(file).click();
		document.querySelector(file).addEventListener("change", function () {
			let reader = new FileReader();
			reader.onloadend = function (e) {
				qaq = e.target.result;
				if (typeof callback === "function") {
					callback(qaq);
				}
			};
			reader.readAsDataURL(this.files[0]);
		});
	},
	//注册接口
	register(url, data) {
		return axios({
			method: "post",
			url,
			data,
		})
			.then((result) => {
				console.log(result);
				return Promise.resolve(result);
			})
			.catch((err) => console.log(err));
	},
	//创建列表
	createOption(url, el, name) {
		axios
			.get(url)
			.then((res) => {
				let { verify, msg, ...content } = res.data;
				if (verify) {
					this.get_object_first_attribute(content).forEach((item) => {
						let option = document.createElement("option");
						option.innerHTML = item[name];
						option.value = item.id;
						option.name = "role";
						document.querySelector(el).appendChild(option);
					});
				}
			})
			.catch((err) => console.log(err));
	},
	//文章发布接口
	publish_article(data) {
		return axios.post("https://ty.php800.cn/api/article/post.php", data, {
			headers: {
				// TOKEN: localStorage.getItem("token"),
			},
		});
	},
	//定时关闭
	timeout(el) {
		setTimeout(() => {
			document.querySelector(el).style.display = "none";
		}, 2000);
	},
	//文章列表
	paper_queries() {
		return axios.get("https://ty.php800.cn/api/article/list.php");
	},
	//时间戳转换
	getdate(date) {
		var now = new Date(date),
			y = now.getFullYear(),
			m = now.getMonth() + 1,
			d = now.getDate();
		return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
	},
	//获取单用户信息
	getone(id) {
		let formdata = new FormData();
		formdata.append("id", id);
		return axios
			.get("https://ty.php800.cn/api/member/search.php", {
				params: {
					id,
				},
			})
			.then((result) => {
				let { verify, msg, member } = result.data;
				if (verify) {
					return member;
				} else {
					console.log(msg);
				}
			})
			.catch((err) => console.log(err));
	},
	//获取本地id
	getLocalStorageId() {
		return JSON.parse(localStorage.getItem("user")).id;
	},
	//获取分类列表
	listOfCategories() {
		axios
			.get("https://ty.php800.cn/api/article/classshow.php")
			.then((result) => {
				let { verify, msg, classType } = result.data;
				if (verify) {
					let list_group = document.querySelector(".list-group");
					list_group.innerHTML = "";
					classType.forEach((item) => {
						let li = document.createElement("li");
						li.classList.add("list-group-item");
						li.value = item.id;
						li.innerHTML = `
                        <span class="badge">删除</span>${item.title}
                        `;
						list_group.appendChild(li);
					});
				} else {
					console.log(msg);
				}
			})
			.catch((err) => console.log(err));
	},
	//提示框
	popUpBox(verify, msg) {
		document.querySelector("#myAlert").children[1].innerHTML = msg;
		document.querySelector("#myAlert").style.display = "block";
		this.timeout("#myAlert");
	},
	//角色列表
	getRoleList() {
		axios
			.get("https://ty.php800.cn/api/member/role.php")
			.then((result) => {
				let { verify, msg, role } = result.data;
				if (verify) {
					classifly_list.innerHTML = `<li class="list-group-item">
					<p>id</p>
					角色名
				</li>`;
					role.forEach((item) => {
						let li = document.createElement("li");
						li.classList.add("list-group-item");
						li.innerHTML = `<p>${item.id}</p>
						<span class="badge del">删除</span><span class="badge change">编辑</span>${item.rolename}`;
						classifly_list.appendChild(li);
					});
				} else {
					console.log(msg);
				}
			})
			.catch((err) => console.log(err));
	},
	get_object_first_attribute(data) {
		for (var key in data) return data[key];
	},
	//设置指定对象的value值
	setVal(el, value) {
		document.querySelector(el).value = value;
	},
	//设置默认选中 设置typeWieselected则是下拉框的选中，默认是复选或单选
	setchecked(el, value, type = "checked") {
		if (type === "checked") {
			document.querySelectorAll(el).forEach((item) => {
				if (item.value === value) {
					item.checked = "checked";
				}
			});
		} else if (type === "selected") {
			document.querySelectorAll(el).f4orEach((item) => {
				if (item.value === value) {
					item.selected = "selected";
				}
			});
		}
	},
	//单选按钮设置formdata
	setformdata(el, calback) {
		document.querySelectorAll(el).forEach((item) => {
			item.addEventListener("click", () => {
				if (typeof calback === "function") {
					calback(item.value, item);
				}
			});
		});
	},
	//获取地址中的id
	getlocationId() {
		let url = new URLSearchParams(location.search.substring(1));
		//判断是否传递了id，没有传递id则从本地取
		if (url.get("id") === null) {
			return this.getLocalStorageId();
		} else {
			return url.get("id");
		}
	},
	//获取单对象的value
	getVal(el) {
		return document.querySelector(el).value;
	},
	getclassifyname(id) {
		return axios({
			method: "get",
			url: "https://ty.php800.cn/api/article/classshow.php",
			params: {
				id,
			},
		}).then((res) => {
			return res.data.classType;
		});
	},
	//判断是否有权限
	jurisdiction() {
		axios
			.get("https://ty.php800.cn/api/token.php", {
				headers: {
					TOKEN: `${localStorage.getItem("token")}`,
					SAT: "1234567",
				},
			})
			.then((result) => {
				let { verify } = result.data;
				if (!verify) {
					top.location.href = "login.html";
				}
			})
			.catch((err) => console.log(err));
	},
	//密码显示和隐藏
	hide_show(eye, password) {
		let chane_eye = document.querySelector(eye),
			input = document.querySelector(password);
		chane_eye.onclick = function () {
			if (input.type == "text") {
				input.type = "password";
				chane_eye.classList.add = "glyphicon-eye-open"; //glyphicon-eye-close
				chane_eye.classList.remove = "glyphicon-eye-close";
			} else {
				input.type = "text";
				chane_eye.classList.add = "glyphicon-eye-close"; //glyphicon-eye-close
				chane_eye.classList.remove = "glyphicon-eye-open";
			}
		};
	},
};

/*
 */
